/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;


import java.util.ArrayList;
import java.util.EnumSet;
import java.util.EventObject;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.ModelViewPage;
import fi.honkalampisaatio.lmodeller.ui.Model3DView.VIEWMODES;
import glapp.GLApp;

public class ViewModel extends GameView {
	Model3DView modelView;
	
	//GameTextLabel titleLabel, instructionLabel;
	//GameButton prevButton, nextButton, backButton, homeButton;
	
	int page;
	ArrayList<ModelViewPage> pages;
	
	int oldMouseX, oldMouseY;
	float zoom;

	public ViewModel() {
		super(EnumSet.of(BUTTONFLAGS.BACK, BUTTONFLAGS.HOME, BUTTONFLAGS.PREVIOUS, BUTTONFLAGS.NEXT, 
				BUTTONFLAGS.DOWNLABEL, BUTTONFLAGS.UPLABEL, BUTTONFLAGS.VOICE));
	}
	
    public void init()
    {
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	
    	modelView = new Model3DView(new Rectangle(w, h, w*8, h*8));
    	modelView.setup();
    	super.addComponent(modelView);

    	page = 0;
		oldMouseX = -1;
		oldMouseY = -1;
		zoom = 1;

    	setPageCount(0);
    }

	public void mouseDown(int x, int y) {
    	super.mouseDown(x, y);

		oldMouseX = x;
		oldMouseY = y;
	}
	

	public void mouseUp(int x, int y) {
    	super.mouseUp(x, y);
		oldMouseX = -1;
		oldMouseY = -1;
	}

	
    public void mouseMove(int x, int y) {
    	super.mouseMove(x, y);
        if(modelView.getViewMode()==VIEWMODES.MANUAL && 
        		(oldMouseX>=0 && oldMouseY>=0))
        {
    		modelView.addRotate((oldMouseY-y)*300/GLApp.getHeight(),
    				(x-oldMouseX)*300/GLApp.getWidth(), 0);
    		oldMouseX = x;
    		oldMouseY = y;
        }
    }
    
	public void setPages(ArrayList<ModelViewPage> pages) {
		page = 0;
		this.zoom = 1;
		modelView.setZoom(this.zoom);
		this.pages = pages;
		if(pages!=null && page<pages.size())
		{
			modelView.setModel(pages.get(page).getModel(), true);
			setUpLabelText(pages.get(page).getText());
			if(modelView.getModel()!=null)
			{
				setDownLabelText(modelView.getModel().getName());
			}
			else
			{
				setDownLabelText("");
			}

			setUpToolTipSentence(pages.get(page).getVoiceText());
		}
		else
		{
			EventObject se = new EventObject(this);
			this.fireSelectionEvent(se);
		}
		oldMouseX = -1;
		oldMouseY = -1;
    	setPageCount(pages.size());
		
	}

	public void setViewMode(VIEWMODES viewMode) {
		modelView.setViewMode(viewMode);
	}

	public void rotate(int x, int y, int z) {
		if(modelView.getViewMode()==VIEWMODES.MANUAL)
		{
			modelView.addRotate(x, y, z);
		}
	}

	public void toggleDebugMode() {
		modelView.toggleDebugMode();
	}

	public void zoomIn() {
		this.zoom = this.zoom * 1.1f;
		modelView.setZoom(this.zoom);
	}

	public void zoomOut() {
		this.zoom = this.zoom * 0.9f;
		modelView.setZoom(this.zoom);
	}

	@Override
	protected void refreshPage(int page) {
		if(pages!=null &&  page<pages.size())
		{
			modelView.setModel(pages.get(page).getModel(), false);
			setUpLabelText(pages.get(page).getText());
			if(modelView.getModel()!=null)
			{
				setDownLabelText(modelView.getModel().getName());
			}
			else
			{
				setDownLabelText("");
			}
			setUpToolTipSentence(pages.get(page).getVoiceText());
		}
	}

	
}
